/**
 * 删除c程序中的所有注释
 */

#include <stdio.h>

#define MAXLINE 1000
#define IN  1
#define OUT 0

int getlines(char s[], int lim);

int main() {
    int c, state, len;
    char line[MAXLINE];

    state = OUT;
    while ((len= getlines(line, MAXLINE)) > 0) {
        //printf("%s", line);
        int i;
        for (i = 0; i < len; i++) {
            //printf("line[%d]=%c, state=%d\n", i, line[i], state);
            if(line[i] == '\'' || line[i] == '\"') state ^= IN;
            if(state == OUT && line[i] == '/' && line[i+1] == '/') {
                line[i] = '\0';
            }
        }
        printf("%s", line);
    }

    return 0;
}



// 从输出中获取内容凑成一行直到遇到换行符或者达到lim限制长度
int getlines(char s[], int lim) {
    int i, c;
    for( i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; i++) {
        s[i] = c;
    }
    if (c == '\n') {
        s[i] = '\n';
        i++;
    }
    s[i] = '\0';

    return i;
}

